<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';

class Honda_code extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Hondacustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_honda_code_type;
        $data['code_type'] = array4js($g_honda_code_type);

        $this->load->view('admin/honda_code',$data);
    }

    public function getCodeList() {
        $assist['where'] = getFilter($this->input->post(),Hondacustomermodel::$codeSearchField);
        $assist['like'] = getFilter($this->input->post(),Hondacustomermodel::$codeLikeField);
        $assist['pagination'] = getPagination($this->input->post());

        $assist['select'] = '
            honda_customer.customer_id,
            honda_customer.openid,
            honda_code.*
        ';
        list($list,$count) = $this->m_customer->getCodeListSection($assist);

        // foreach ($list as $key => $value) {
        //     $list[$key]['nickname'] = urldecode($value['nickname']);
        // }

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function importList() {
        $this->validate->setRule('type','integer');
		$this->validate->setRule('file','file');
        $this->validate->setRule('file','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $this->load->library('zmexcel');
        $this->zmexcel->checkExcelFile($info['file']);
        $path = $this->m_app->saveFile($info['file'],array(),UPLOAD_PATH_FILE);
        $this->zmexcel->init(UPLOAD_PATH_FILE.$path);

        @set_time_limit(0);
        @ini_set('memory_limit', '2048M');
        $data = $this->zmexcel->getDatas();

        $this->load->database();
        $assist = array();
        list($list,$count) = $this->m_customer->getCodeListSection($assist);
        $list = reindexArray($list,'code_number');

        $insert_arr = array();
        $update_arr = array();

        $existed = array();
        global $g_honda_code_type;
        if(!isset($g_honda_code_type[$info['type']])) {
            $this->response->setDetail('类型错误！');
            $this->response->responseExit();
        }
        // $type_arr = array_flip($g_honda_code_type);
        foreach ($data as $k => $record) {
            foreach($record as $key => $value) {
                if(!$value || $value==null) $record[$key] = '';
                $record[$key] = trim($record[$key]);
            }
            $k += 2;
            $code_number = $record['A'];
            if($code_number == '' || isset($existed[$code_number])) continue;
          //   if(!isset($type_arr[$record['A']])) {
          //   	$this->response->setDetail("第{$k}行列表[{$record['A']}]不存在!!!");
        		// $this->response->responseExit();
          //   }
            $data = array(
            	'code_number' => $code_number,
            	// 'code_code' => $record['C'],
            	'code_type' => $info['type'],
            );
            if(isset($list[$code_number])) {
            	if($list[$code_number]['code_status'] == 1) {
            		$this->response->setDetail("第{$k}行编码[{$code_number}]已被激活，不能导入!!!");
        			$this->response->responseExit();
            	}
                $update_arr[] = $data;
            } else {
            	$data['code_time_create'] = NOW;
            	$insert_arr[] = $data;
            }
            $existed[$code_number] = 1;
        }
        $this->m_customer->createCodeBatch($insert_arr);
        $this->m_customer->updateCodeBatch($update_arr);
        $this->db->close();

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
	}

}
/* End of file aa_game_history.php */
/* Location: ./application/controllers/aa_game_history.php */